package com.abc.springboot.controller;

import com.abc.springboot.mapper.StudentMapper;
import com.abc.springboot.pojo.MyList;
import com.abc.springboot.pojo.Student;
import com.abc.springboot.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @RequestMapping("/selectAll")
    public List<Student> allStudent(){
        List<Student> studentList =studentService.queryStudent();
        return studentList;
    }

    @RequestMapping("/selectById")
    public Student studentById(int id){
        Student student=studentService.queryStudentById(id);
        return student;
    }

    @RequestMapping("/selectByName")
    public Student studentByName(String name){
        Student student=studentService.queryStudentByName(name);
        return student;
    }

    @RequestMapping("/selectByPhonenumber")
    public Student studentByPhonenumber(long phonenumber){
        Student student=studentService.queryStudentByPhonenumber(phonenumber);
        return student;
    }

    //http://localhost:8080/student/addAStudent?id=9&name=王五&age=15&classId=3
    @RequestMapping("/addAStudent")
    public List<Student> addAStudent(@RequestBody Student student){
//        System.out.println("  ghvhv");
        studentService.addAStudent(student);
        return studentService.queryStudent();
    }

    //http://localhost:8080/student/addStudents
    @RequestMapping("/addStudents")
    public String addStudents(@RequestBody Student[] students){
        List<Student> list= Arrays.asList(students);
        for(Student s:list){
            System.out.println(s.getId());
            System.out.println(s.getName());
        }
        studentService.addStudents(list);
        return "插入数据成功";
    }
}
